package client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Peter on 2017/6/30.
 */
public class ClientHandler extends ChannelHandlerAdapter {

    // Add the text line codec combination first,
    String delimiter = "$_";
    private static Logger logger = LoggerFactory.getLogger(ClientHandler.class);

    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        logger.info("channelActive --- " + ctx.channel().remoteAddress().toString());
        String student = "{\n" +
                "    \"mustParams\":{\n" +
                "        \"Timestamp\":\"1499851528\",\n" +
                "        \"user_type\":\"1\",\n" +
                "        \"token\":\"6d125420-ac38-41b0-8673-eb53125b0d5a\",\n" +
                "        \"client_type\":\"2\",\n" +
                "        \"version\":\"11\",\n" +
                "        \"SignInfo\":\"3e86af83a6c96be55461baa344d3b2f5\"\n" +
                "    },\n" +
                "    \"functionName\":\"student\"\n" +
                "}" + delimiter;
        ctx.writeAndFlush(student);

        Thread.sleep(1000);

        String wakeUp = "{\n" +
                "    \"params\":{\n" +
                "        \"called_user_id\":\"3474\",\n" +
                "        \"cou_id\":\"100\"\n" +
                "    },\n" +
                "    \"mustParams\":{\n" +
                "        \"SignInfo\":\"71189135b50c3b7c76dfffa11ad5cdf7\",\n" +
                "        \"user_type\":\"1\",\n" +
                "        \"token\":\"6d125420-ac38-41b0-8673-eb53125b0d5a\",\n" +
                "        \"client_type\":\"2\",\n" +
                "        \"Timestamp\":\"1499825322\",\n" +
                "        \"version\":\"11\"\n" +
                "    },\n" +
                "    \"functionName\":\"wakeupCalled\"\n" +
                "}" + delimiter;
        ctx.writeAndFlush(wakeUp);
    }

    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.info("断开连接 channelInactive --- " + ctx.channel().remoteAddress().toString());
    }
}
